package cn.gjxblog.validation.hibernate;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationProviderResolver;
import javax.validation.spi.ValidationProvider;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * 校验入口
 * {@link Validation}
 *
 * 配置入口
 * @see HibernateValidatorConfiguration
 */
@RunWith(JUnit4.class)
public class TestHibernateValidate {

    /**
     * Validation 校验入口
     * 方式一：
     *   Validation.buildDefaultValidatorFactory()
     */
    @Test
    public void testValidation(){
        Person person = new Person();
        person.setAge(-1);
        person.setEmails(Arrays.asList("guojiaxun@fang.cm","123455"));

        Set<ConstraintViolation<Person>> result =
                Validation.buildDefaultValidatorFactory()
                        .getValidator()
                        .validate(person);

        //对结果遍历输出
        result.stream()
                .map(v -> v.getPropertyPath() + " " + v.getMessage() + ":" + v.getInvalidValue())
        .forEach(System.out::println);
    }

    /**
     * 方式二
     *Validation.byDefaultProvider()
     * 指定自定义的ValidationProviderResolver逻辑选出一个ValidationProvide去完成校验
     */
    @Test
    public void testValidation2(){
        Person person = new Person();
        person.setAge(-1);
        person.setEmails(Arrays.asList("guojiaxun@fang.cm","123455"));

        //需要自己实现ValidationProviderResolver
        Set<ConstraintViolation<Person>> result =
                Validation.byDefaultProvider().providerResolver(new ValidationProviderResolver(){
                    @Override
                    public List<ValidationProvider<?>> getValidationProviders() {
                        HibernateValidator hibernateValidator = new HibernateValidator();
                        return Arrays.asList(hibernateValidator);
                    }
                }
                ).configure().buildValidatorFactory().getValidator().validate(person);

        //对结果遍历输出
        result.stream()
                .map(v -> v.getPropertyPath() + " " + v.getMessage() + ":" + v.getInvalidValue())
                .forEach(System.out::println);
    }


    /**
     * 方式三
     *
     * Validation.byProvider
     * 指定一个ValidationProvider去完成校验
     */
    @Test
    public void testValidation3(){
        Person person = new Person();
        person.setAge(-1);
        person.setEmails(Arrays.asList("guojiaxun@fang.cm","123455"));

        //指定 ValidationProvider
        Set<ConstraintViolation<Person>> result = Validation.byProvider(HibernateValidator.class)
                .configure()
                .buildValidatorFactory()
                .getValidator()
                .validate(person);

        //对结果遍历输出
        result.stream()
                .map(v -> v.getPropertyPath() + " " + v.getMessage() + ":" + v.getInvalidValue())
                .forEach(System.out::println);
    }




}
